package com.eastfair.auth.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.eastfair.auth.dao.FunctionRoleMapper;
import com.eastfair.auth.entity.FunctionRole;
import com.eastfair.auth.service.FunctionRoleService;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 *
 * </p>
 *
 * @author ligang
 * @date 2021-07-26
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class FunctionRoleServiceImpl extends SuperServiceImpl<FunctionRoleMapper, FunctionRole> implements FunctionRoleService {
    @Autowired
    ProjectCoreServiceFeign projectCoreServiceFeign;

    /**************sharding 分库分表操作 *****************/
    @Override
    public boolean updateByIdOfShard(FunctionRole entity) {
        UpdateWrapper<FunctionRole> updateWrapper = new UpdateWrapper<>();
        //可将指定字段更新为null
        updateWrapper.lambda().eq(FunctionRole::getProjectId, entity.getProjectId()).eq(FunctionRole::getId, entity.getId());
        entity.setProjectId(null);
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(FunctionRole model) {
        //雪花ID
        model.setId(DefaultUidGenerator.getUidOfProjectId(ContextUtil.getProjectId()));
        model.setProjectId(ContextUtil.getProjectId());
        return R.successDef();
    }


    @Override
    public R addOfFunction(Long roleId, List<Long> funcntionIds) {
        if (funcntionIds != null && !funcntionIds.isEmpty()) {
            FunctionRole functionRole = new FunctionRole();
            functionRole.setRoleId(roleId);
            List<FunctionRole> menuRoleList = list(functionRole, null, null);
            //剔除已经存在的
            if (menuRoleList != null && !menuRoleList.isEmpty()) {
                List<Long> oldFunctionIds = menuRoleList.stream().map(FunctionRole::getFunctionId).collect(Collectors.toList());
                funcntionIds.removeAll(oldFunctionIds);
            }
            if (funcntionIds != null && !funcntionIds.isEmpty()) {
                for (Long functionid : funcntionIds) {
                    FunctionRole functionRole1 = new FunctionRole();
                    functionRole1.setFunctionId(functionid);
                    functionRole1.setRoleId(roleId);
                    save(functionRole1);
                }
            }
        }
        return R.success();
    }

    @Override
    public R removeFunctionRole(Long funcntionId, List<Long> roleIds) {
        FunctionRole functionRole = new FunctionRole();
        functionRole.setFunctionId(funcntionId);
        List<FunctionRole> functionRoleList = list(functionRole, null, roleIds);
        if (functionRoleList != null && !functionRoleList.isEmpty()) {
            for (FunctionRole functionRole1 : functionRoleList) {
                functionRole1.setIsDeleted(BusinessConstant.DELETE_YES);
                updateByIdOfShard(functionRole1);
            }
        }
        return R.success();
    }

    @Override
    public R removeRole(List<Long> roleIds) {
        List<FunctionRole> functionRoleList = list(null, null, roleIds);
        if (functionRoleList != null && !functionRoleList.isEmpty()) {
            for (FunctionRole functionRole : functionRoleList) {
                functionRole.setIsDeleted(BusinessConstant.DELETE_YES);
                updateByIdOfShard(functionRole);
            }
        }
        return R.success();
    }

    @Override
    public R removeFunction(List<Long> funcntionIds) {
        List<FunctionRole> functionRoleList = list(null, funcntionIds, null);
        if (functionRoleList != null && !functionRoleList.isEmpty()) {
            for (FunctionRole functionRole : functionRoleList) {
                functionRole.setIsDeleted(BusinessConstant.DELETE_YES);
                updateByIdOfShard(functionRole);
            }
        }
        return R.success();
    }

    @Override
    public R<List<FunctionRole>> findOfFunction(List<Long> funcntionIds) {
        return R.success(list(null, funcntionIds, null));
    }

    @Override
    public List<FunctionRole> list(FunctionRole functionRole, List<Long> funcntionIds, List<Long> roleIds) {
        //查询
        QueryWrapper<FunctionRole> queryWrapper = new QueryWrapper<>();
        if (functionRole != null) {
            if (functionRole.getId() != null) {
                queryWrapper.lambda().eq(FunctionRole::getId, functionRole.getId());
            }
            if (functionRole.getRoleId() != null) {
                queryWrapper.lambda().eq(FunctionRole::getRoleId, functionRole.getRoleId());
            }

            if (functionRole.getFunctionId() != null) {
                queryWrapper.lambda().eq(FunctionRole::getFunctionId, functionRole.getFunctionId());
            }
        }
        if (ContextUtil.getProjectId() != null) {
            queryWrapper.lambda().eq(FunctionRole::getProjectId, ContextUtil.getProjectId());
        }


        if (funcntionIds != null && !funcntionIds.isEmpty()) {
            queryWrapper.lambda().in(FunctionRole::getFunctionId, funcntionIds);
        }
        if (roleIds != null && !roleIds.isEmpty()) {
            queryWrapper.lambda().in(FunctionRole::getRoleId, roleIds);
        }
        queryWrapper.lambda().eq(FunctionRole::getIsDeleted, BusinessConstant.DELETE_NO);
        List<FunctionRole> menuRoleList = list(queryWrapper);
        return menuRoleList;
    }
}
